package com.gdsz.libbasic.utils.system

import android.annotation.SuppressLint
import android.content.Context
import android.location.*
import android.os.Build
import java.math.BigDecimal
import java.math.RoundingMode


class GpsTool(block: (signal: Float) -> Unit) {
    private var manager: LocationManager? = null

    @SuppressLint("MissingPermission")
    fun initLocationGps(
        context: Context
    ) {

        manager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

            manager!!.registerGnssStatusCallback(gnssCallback)
        } else {

            manager!!.addGpsStatusListener(gpsStatusListener)
        }


        manager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1f, locationListener)


    }

    private val locationListener: LocationListener = LocationListener {


    }


    fun releaseLocationGps() {
        manager?.let {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                it.unregisterGnssStatusCallback(gnssCallback)
            } else {
                it.removeGpsStatusListener(gpsStatusListener)
            }
            if (manager != null) {
                manager!!.removeUpdates(locationListener)
            }
        }

    }


    private val gnssCallback: GnssStatus.Callback = object : GnssStatus.Callback() {
        override fun onFirstFix(ttffMillis: Int) {
            super.onFirstFix(ttffMillis)
        }

        override fun onStarted() {
            super.onStarted()
        }

        override fun onStopped() {
            super.onStopped()
        }

        override fun onSatelliteStatusChanged(status: GnssStatus) {
            super.onSatelliteStatusChanged(status)

            var signe = 0f
            var count = 0
            repeat(status.satelliteCount) {
                if (status.usedInFix(it)) {
                    signe += status.getCn0DbHz(it)
                    count++
                }

            }

            if (count == 0) {
                block.invoke(0f)
            } else {
                val value = BigDecimal(signe.toString()).divide(
                    BigDecimal(count.toString()),
                    1,
                    RoundingMode.DOWN
                ).toFloat()
                block.invoke(value)
            }


        }
    }


    @SuppressLint("MissingPermission")
    private val gpsStatusListener = GpsStatus.Listener { event ->
        when (event) {
            GpsStatus.GPS_EVENT_SATELLITE_STATUS -> {
                //获取当前状态
                val gpsStatus = manager!!.getGpsStatus(null)
                //获取卫星颗数的默认最大值
                val maxSatellites = gpsStatus!!.maxSatellites
                //获取所有的卫星
                val iters: Iterator<GpsSatellite> = gpsStatus.satellites.iterator()
                //卫星颗数统计
                var count = 0

                var signe = 0f
                while (iters.hasNext() && count <= maxSatellites) {
                    count++
                    val s = iters.next()
                    //卫星的信噪比
                    val snr = s.snr
                    signe += snr
                }


                if (count == 0) {
                    block.invoke(0f)

                } else {
                    val value = BigDecimal(signe.toString()).divide(
                        BigDecimal(count.toString()),
                        1,
                        RoundingMode.DOWN
                    ).toFloat()
                    block.invoke(value)
                }


            }
            else -> {}
        }
    }


}